package tienmanh.gs.goldminer.sprite;

import android.util.Log;
import java.io.PrintStream;

import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.modifier.AlphaModifier;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.modifier.PathModifier;
import org.andengine.entity.modifier.PathModifier.Path;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.primitive.Line;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.color.Color;
import org.andengine.util.math.MathUtils;
import org.andengine.util.modifier.IModifier;

import tienmanh.gs.goldminer.manager.ResourcesManager;
import tienmanh.gs.goldminer.manager.SceneManager;
import tienmanh.gs.goldminer.scene.GameScene;
import tienmanh.gs.goldminer.util.Util;

public class Hook extends AnimatedSprite
{
	Line line1;
@Override
	protected void onManagedUpdate(float pSecondsElapsed) {
		// TODO Auto-generated method stub
		super.onManagedUpdate(pSecondsElapsed);
		rotate();
		float[] arrayOfFloat = convertLocalToSceneCoordinates(14.5f, 10.0F);
        float f1 = arrayOfFloat[0];
        float f2 = arrayOfFloat[1];
//        if(!GameScene.pullGold){
//        	isCheck = true;
//         line.setVisible(true);
		 line1.setPosition(rXo+14.5f, rYo+1, f1, f2);
//		 line.setPosition(f1,f2,value1,value2);
//        }
//        else
//        {
//        	GameScene.line.setPosition( rXo+10f , rYo+1 , GameScene.arrayGolds.get(GameScene.getInstace().t).getX() + GameScene.arrayGolds.get(GameScene.getInstace().t).getWidth()/2 , GameScene.arrayGolds.get(GameScene.getInstace().t).getY());	
//        	line.setVisible(false);
//        	pullHookAfterMine(f1, f2);
//        }
       
	}
//public void pullHookAfterMine(float f1, float f2){
////	if(GameScene.getInstace().isUseMine)
//		GameScene.line.setPosition(rXo+10f, rYo+1,f1,f2);
//}

  SequenceEntityModifier entityModifier;
  private int height;
  public boolean isCheck = true;
  private boolean isPullBackHook;
  private boolean isRotateHook;
  private LoopEntityModifier lemPullBackHook;//
  private LoopEntityModifier lemRotate;
  private LoopEntityModifier lemThrowHook;
  public int r;
  public int rX;
  public int rY;
  public float rXo;
  public float rYo;
  public int startX;
  public int startY;
  private Util util;
  private int width;
  private Line line;
  private float x;
  private float y;

  public Hook(float paramFloat1, float paramFloat2, float paramFloat3, float paramFloat4, ITiledTextureRegion paramITiledTextureRegion, VertexBufferObjectManager paramVertexBufferObjectManager, int paramInt1, int paramInt2 , Line line1)
  {
    super(paramFloat1, paramFloat2, paramFloat3, paramFloat4, paramITiledTextureRegion, paramVertexBufferObjectManager);
    this.width = paramInt1;
    this.height = paramInt2;
    rXo = paramFloat1;
    rYo = paramFloat2;
    setRotationCenter(getWidth() / 2.0F, 1.0F);
    line = new Line(0, 0, 0, 0, paramVertexBufferObjectManager);
    line.setColor(Color.WHITE);
    line.setLineWidth(1f);
    GameScene.getInstace().sceneHUD.attachChild(line);
    util = new Util();
    this.line1 = line1;
//    setScale(0.3f);
    
  }

  public boolean collidesWith(Object paramGolds)
  {
    return (this.util.getDistance(this, paramGolds) + paramGolds.getWidth() / 5.0F < getWidth() / 2.0F + paramGolds.getWidth() / 2.0F) || (this.util.getDistance(this, paramGolds) < getHeight() / 2.0F + paramGolds.getHeight() / 2.0F);
  }
  

  public void flingHook(final float pX, final float pY , long value)
  {
//    unRotate();
    this.x = value1;
    this.y = value2;
    int offRealX = (int) (x - this.getX());
	int offRealY = (int) (y - this.getY());
	float length = (float) Math.sqrt((offRealX * offRealX) + (offRealY * offRealY));
	float velocity = value / 1.0f; 
	float realMoveDuration = length / velocity;
//    Log.i("STARTEND", "STARTEND:" + this.x + ":" + this.y);//
    this.lemThrowHook = new LoopEntityModifier(new PathModifier(realMoveDuration, new PathModifier.Path(2).to(getX(), getY()).to( x,  y)), 1);
    registerEntityModifier(this.lemThrowHook);
	
  }

  public Hook getInstance()
  {
    return this;
  }

  public float getStartX()
  {
    return this.startX;
  }

  public float getStartY()
  {
    return this.startY;
  }



  public boolean isRotateHook()
  {
    return this.isRotateHook;
  }

  public SequenceEntityModifier entityModifier1;
  public void pullHook(float paramFloat1, float paramFloat2, float paramFloat3)
  {
	  setCurrentTileIndex(1);
	  int offRealX = (int) (rXo - paramFloat1);
		int offRealY = (int) (rYo - paramFloat2);
		float length = (float) Math.sqrt((offRealX * offRealX) + (offRealY * offRealY));
		float velocity = paramFloat3 / 1.0f; 
		float realMoveDuration = length / velocity;
     entityModifier1 = new SequenceEntityModifier(
			new IEntityModifierListener() {
				@Override
				public void onModifierStarted(
						final IModifier<IEntity> pModifier,
						final IEntity pItem) {
				}

				@Override
				public void onModifierFinished(
						final IModifier<IEntity> pEntityModifier,
						final IEntity pEntity) {
					try {
					} catch (Exception e) {

					}
				}
			}, new LoopEntityModifier(new PathModifier(realMoveDuration, new PathModifier.Path(2).to(paramFloat1, paramFloat2).to(rXo, rYo)), 1));
	registerEntityModifier(entityModifier1);
  }
  public static int R =15;
  private float XBase;
  private float YBase;
  public static float angle = 10.0F;
  private boolean left = true;
  //
  public void rotate()
  {
	  if(isCheck){
	    if (this.left)
	    {
	      this.angle = (1.5F + this.angle);
	      if (this.angle >= 170.0F)
	        this.left = false;
	    }
	    else{
	      this.angle -= 1.5F;
	      if (this.angle <= 5.5F)
	           this.left = true;
	    }
	    updatePosition(this.angle);
	    updateAngleView(this.angle);
//	    GameScene.getInstace().isFling = false;
//	    GameScene.getInstace().isCheck = false;
	  }
  }//
  private void updateAngleView(float paramFloat)
  {
    setRotation(paramFloat - 90.0F);
  }
public static float value1,value2;
  public void updatePosition(float paramFloat)
  {
    float[] arrayOfFloat = getPostion(this.R, paramFloat);
    setPosition(rXo + arrayOfFloat[0],  rYo + arrayOfFloat[1]);
    value1 = rXo+14.5f + arrayOfFloat[0]*50;
    value2 = rYo+1 + arrayOfFloat[1]*50;
  }
  public float[] getPostion(float paramFloat1, float paramFloat2)
  {
    double d = Math.PI * paramFloat2 / 180.0D;
    float[] arrayOfFloat = new float[2];
    arrayOfFloat[0] = (float)(paramFloat1 * Math.cos(d));
    arrayOfFloat[1] = (float)(paramFloat1 * Math.sin(d));
    return arrayOfFloat;
  }


  public void unFlingHook()
  {
    unregisterEntityModifier(this.lemThrowHook);
  }

  public void unPullHook()
  {
    unregisterEntityModifier(this.entityModifier1);
  }

  public void unRotate()
  {
    this.isCheck = false;
  }
  public void Rotate()
  {
    this.isCheck = true;
  }

}
